﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using System.Data;
using System.Threading.Tasks;


namespace produccion.Conexion
{
     
        public class OracleConexion
        {

            private readonly string _strcon;
            public static string strUsu;
            public static string strCla;
            private MDOgeneral _mod = new MDOgeneral();

            public OracleConexion(string strcon)
            {
                _strcon = strcon;
            }

            public T EjecutarSp<T>(string spName,List<OracleParameter> parametros)
            {                
                OracleConnection conexion=null;
               
                try
                {
                    if (MDOgeneral.conexion == null || MDOgeneral.conexion.State == ConnectionState.Closed)
                    {
                        MDOgeneral.conectar();
                    }
                    conexion = new OracleConnection(MDOgeneral.strConexionBaseDeDatos);
                    conexion.Open();
                    

                    var cmd = new OracleCommand(spName,conexion) { CommandType = CommandType.StoredProcedure };
                    cmd.CommandTimeout = 0;
                    if (parametros != null)
                    {
                        cmd.Parameters.AddRange(parametros.ToArray());
                    }
                                 
                    //valido el tipo de dato a retornar
                    if (typeof(T) == typeof(DataSet))
                    {
                        var ds = new DataSet();
                        using (var adapter = new OracleDataAdapter(cmd))
                        {
                            adapter.Fill(ds);
                        }
                        return (T)(Object)ds;
                    }

                    if (typeof(T) == typeof(DataTable))
                    {
                        var ds = new DataTable();
                        using (var adapter = new OracleDataAdapter(cmd))
                        {
                            adapter.Fill(ds);
                        }
                        return (T)(Object)ds;
                    }

                    if (typeof(T) == typeof(OracleDataReader))
                    {
                        var reader = cmd.ExecuteReader();
                        return (T)(Object)reader;
                    }

                    if (typeof(T) == typeof(int))
                    {
                        var entero = cmd.ExecuteNonQuery();
                        return (T)(Object)entero;
                    }

                    if (typeof(T) == typeof(bool))
                    {
                        bool retorno = Convert.ToBoolean(cmd.ExecuteNonQuery());
                        return (T)(object)retorno;
                    }

                    if (typeof(T) == typeof(string))
                    {
                        string retorno = cmd.ExecuteNonQuery().ToString();
                        return (T)(Object)retorno;
                    }

                    throw new NotSupportedException(string.Format("El tipo {0} no es soportado", typeof(T).Name));
                }
                finally
                {
                    if (conexion != null && conexion.State == ConnectionState.Open)
                    {
                        conexion.Close();
                    }
                }
            }
        }


    
}
